
package derby;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.TypedQuery;

import derby.DatabaseConfigFactory.Database;
import derby.DatabaseConfigFactory.IDatabaseConfig;
import entities.User;

public class DerbyTest {

	
	public static void main(String[] args) throws SQLException {
	  
		IDatabaseConfig databaseConfig = DatabaseConfigFactory.createDatabaseConfig(Database.DERBY_IN_MEMORY);
		
		String password = "password";
		
		String user = "user";
		
		Map<String, String> properties = new HashMap<String, String>(databaseConfig.getProperties());
		
		properties.put("javax.persistence.jdbc.driver", databaseConfig.getDriver());
		properties.put("javax.persistence.jdbc.url", databaseConfig.getUrl("localhost", "teste"));
		properties.put("javax.persistence.jdbc.user", user);
		properties.put("javax.persistence.jdbc.password", password);
		
		properties.put("hibernate.dialect", databaseConfig.getDialectClassName());
		
		EntityManagerFactory emf = javax.persistence.Persistence.createEntityManagerFactory("PU",properties);	
		
		EntityManager em = emf.createEntityManager();
		
		TypedQuery<User> createQuery = em.createQuery("select t from User t", User.class);
		
		System.out.println("Result: "+createQuery.getResultList());
		
		em.close();
		emf.close();
		
		System.out.println("Terminou");
		
  }
	
}
